﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MYTech.Utils
{
    /// <summary>
    /// 加解密类型
    /// </summary>
    public enum DesType
    {
        /// <summary>
        /// 加密
        /// </summary>
        Encrypt,
        /// <summary>
        /// 解密
        /// </summary>
        Decrypt
    }

    /// <summary>
    /// DES算法加密解密
    /// </summary>
    public class DESHelper
    {
        /// <summary>
        ///加密转换 
        ///type:true表示加密，false表示解密
        /// </summary>
        /// <param name="oldInfo">原字符串</param>
        /// <param name="newInfo">新字符串</param>
        /// <param name="type">true表示加密，false表示解密</param>
        public static void DESTranslate(string sourceVal, out string destVal, DesType type)
        {
            destVal = null;
            string myKey = "abcdefgh";
            if (type == DesType.Encrypt)
            {
                destVal = Encrypt(sourceVal, myKey);
            }
            else if (type == DesType.Decrypt)
            {
                destVal = Decrypt(sourceVal, myKey);
            }
        }

        public static string EncryptPassword(string password)
        {
            string val = null;
            DESTranslate(password, out val, DesType.Encrypt);
            return val;
        }

        public static string DecryptPassword(string password)
        {
            string val = null;
            DESTranslate(password, out val, DesType.Decrypt);
            return val;
        }

        #region 加密方法
        /// <summary>   
        /// 加密方法 
        /// </summary> 
        /// <param name="encryptVal">需要加密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string encryptVal, string key)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中           
                //原来使用的UTF8编码，我改成Unicode编码了，不行   
                byte[] inputByteArray = Encoding.Default.GetBytes(encryptVal);
                //建立加密对象的密钥和偏移量           
                //使得输入密码必须输入英文文本         
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch
            {
            }
            return "";
        }
        #endregion

        #region 解密方法
        /// <summary>   
        /// 解密方法  
        /// </summary>  
        /// <param name="decryptVal">需要解密的字符串</param>   
        /// <param name="key">密匙</param>  
        /// <returns>解密后的字符串</returns> 
        public static string Decrypt(string decryptVal, string key)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[decryptVal.Length / 2];
                for (int x = 0; x < decryptVal.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(decryptVal.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
                //建立加密对象的密钥和偏移量，此值重要，不能修改 
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //建立StringBuild对象，CreateDecrypt使用的是流对象，必须把解密后的文本变成流对象       
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
            }
            return "";
        }
        #endregion
    }
}
